﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AzureTosser.Services;
using System.Net;

namespace AzureTosser.Controllers
{
    [JSONPFilter]
    public class SendController: Controller
    {
        public ActionResult Index(string pipe, string topic, string package)
        {
            
            ITossBus bus = TossProviderFactory.Instance.Bus;
            var pipeDef = bus.GetPipeDefinition(pipe, false);
            if (pipeDef == null)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return Json("Pipe not found.", JsonRequestBehavior.AllowGet);
            }
            if (pipeDef.PipeType != PipeType.MulticastSend 
                && pipeDef.PipeType != PipeType.MulticastDuplex
                && pipeDef.PipeType != PipeType.UnicastDuplex
                && pipeDef.PipeType != PipeType.UnicastSend)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return Json("Pipe is not a sending pipe.", JsonRequestBehavior.AllowGet);
            }
            try
            {
                bus.Send(pipeDef, topic, package);
                return Json("OK", JsonRequestBehavior.AllowGet);
            }
            catch (ArgumentException argExp)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return Json(argExp.Message, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return Json("Internal service error.", JsonRequestBehavior.AllowGet);
            }
        }
        [HttpPost]
        public ActionResult Post(string pipe, string topic, string package)
        {
            ITossBus bus = TossProviderFactory.Instance.Bus;
            var pipeDef = bus.GetPipeDefinition(pipe,false);
            if (pipeDef == null)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return Json("Pipe not found.", JsonRequestBehavior.AllowGet);
            }
            if (pipeDef.PipeType != PipeType.MulticastSend)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return Json("Pipe is not a sending pipe.", JsonRequestBehavior.AllowGet);
            }
            try
            {
                bus.Send(pipeDef, topic, package);
                return Json("OK", JsonRequestBehavior.AllowGet);
            }
            catch (ArgumentException argExp)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return Json(argExp.Message, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return Json("Internal service error.", JsonRequestBehavior.AllowGet);
            }
        }
    }
}